package model;

import com.google.gson.annotations.Expose;
import exception.MibException;
import util.common.StringUtils;
import util.mib.MibFormatUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MibNode implements Cloneable{
    @Expose
    public String description;
    @Expose
    public int enterpriseCode;
    @Expose
    public int level; // 层
    @Expose
    public String name;
    @Expose
    public int node = -1;
    @Expose
    public String oid;
    @Expose
    public String moduleName;
    @Expose
    public String parentName;

    public String clazzName; // 节点类型

    public List<MibNode> children = new LinkedList<MibNode>();
    public String value;
    public boolean isTable = false;
    public boolean isTableObject = false;
    public boolean isTableEntity = false;
    public String txt;


    private static MibNode IsoNode;

    static {
        MibNode iso = new MibNode("iso");
        iso.oid = ".1";
        iso.level = 1;
        iso.enterpriseCode = 0;
        iso.node = 1;
        iso.clazzName = "MibNode";
        IsoNode = iso;
    }

    public MibNode() {
    }

    public MibNode(String name) {
        this.name = name;
    }

    /**
     * 获取iso节点
     * @return
     */
    public static MibNode getIsoNode() {
        try {
            return (MibNode) MibNode.IsoNode.clone();
        } catch (CloneNotSupportedException e) {
            throw new MibException();
        }
    }

    public static boolean isIsoNode(String name) {
        if (name == null)
            throw new NullPointerException();
        return name.equals(IsoNode.name);
    }

    // 根据父oid生成当前node节点

    public void generateInfoByParentOid(String parentOid){
        if (this.node == -1)
            throw new MibException("当前节点编号还未初始化，无法生成根据父节点生成节点信息。", this);
        if (parentOid == null) parentOid = "";
        generateInfoByOid(parentOid + "." + this.node);
    }
    public void generateInfoByOid(String currentOid){
        this.oid = currentOid;
        this.enterpriseCode = getEnterpriseCode(oid);
        this.level = StringUtils.splitRemoveEmpty(oid, "\\.").length;
    }

    // 计算企业节点号

    public static int getEnterpriseCode(String oid){
        if (oid == null)
            throw new RuntimeException();
        else if (!oid.startsWith(".")){
            oid = "." + oid;
        }
        String enterprise = ".1.3.6.1.4.1.";
        int code = 0;
        if (oid.startsWith(enterprise) && oid.length() > enterprise.length()){
            Pattern pattern = Pattern.compile("\\d+");
            Matcher matcher = pattern.matcher(oid.substring(13));
            if (matcher.find()){
                code = Integer.parseInt(matcher.group(0));
            }else{
                throw new IllegalStateException(oid);
            }
        }
        return code;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public boolean addChild(MibNode child) {
        if (child.oid.startsWith(oid) && child.oid.length() > oid.length()) {
            children.add(child);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (name != null && !name.trim().equals("")) {
            builder.append(String.format("节点名：%s\n", name));
        }
        builder.append(MibFormatUtils.getFormatNodeInfo(this));
        return builder.toString();
    }
}
